﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.03.16
 * 描 述：ajax操作方法
 */
(function ($, learun) {
    "use strict";
    var httpCode = {
        success: 200,
        fail: 400,
        exception: 500
    };
    var exres = {resp_code: httpCode.exception, resp_msg: '通信异常，请联系管理员！'}
    $.extend(learun, {
        // http 通信异常的时候调用此方法
        httpErrorLog: function (msg) {
            learun.log(msg);
        },
        // http请求返回数据码
        httpCode: httpCode,
        // get请求方法（异步）:url地址,callback回调函数
        httpAsyncGet: function (url, callback) {
            if (url.indexOf('?') > 0) {
                url += ('&access_token=' + config.getToken().access_token);
            } else {
                url += ('?access_token=' + config.getToken().access_token);
            }
            $.ajax({
                url: url,
                type: "GET",
                dataType: "json",
                async: true,
                cache: false,
                success: function (data) {
                    if (data.resp_code == learun.httpCode.exception) {
                        learun.httpErrorLog(data.resp_msg);
                        data.resp_msg = '系统异常，请联系管理员！';
                    }
                    callback(data);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    learun.httpErrorLog(textStatus);
                    callback(exres);
                },
                beforeSend: function () {
                },
                complete: function () {
                }
            });
        },
        httpAsyncGet2: function (url, param, callback) {
            url += ('?access_token=' + config.getToken().access_token + param);
            $.ajax({
                url: url,
                type: "GET",
                dataType: "json",
                async: true,
                cache: false,
                success: function (data) {
                    if (data.resp_code == learun.httpCode.exception) {
                        learun.httpErrorLog(data.resp_msg);
                        data.resp_msg = '系统异常，请联系管理员！';
                    }
                    callback(data);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    learun.httpErrorLog(textStatus);
                    callback(exres);
                },
                beforeSend: function () {
                },
                complete: function () {
                }
            });
        },
        // get请求方法（同步）:url地址,param参数
        httpGet: function (url, param) {
            var res = {};
            $.ajax({
                url: url,
                data: param,
                type: "GET",
                dataType: "json",
                async: false,
                cache: false,
                success: function (data) {
                    if (data.resp_code == learun.httpCode.exception) {
                        learun.httpErrorLog(data.resp_msg);
                        data.resp_msg = '系统异常，请联系管理员！';
                    }
                    res = data;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    learun.httpErrorLog(textStatus);
                },
                beforeSend: function () {
                },
                complete: function () {
                }
            });
            return res;
        },
        // post请求方法（异步）:url地址,param参数,callback回调函数
        httpAsyncPost: function (url, param, callback) {
            $.ajax({
                url: url,
                data: param,
                type: "POST",
                dataType: "json",
                contentType: 'application/json',
                async: true,
                cache: false,
                success: function (data) {
                    if (data.resp_code == learun.httpCode.exception) {
                        learun.httpErrorLog(data.resp_msg);
                        data.resp_msg = '系统异常，请联系管理员！';
                    }
                    callback(data);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    learun.httpErrorLog(textStatus);
                    callback(exres);
                },
                beforeSend: function () {
                },
                complete: function () {
                }
            });
        },
        // post请求方法（同步）:url地址,param参数,callback回调函数
        httpPost2: function (url, param, callback) {
            $.ajax({
                url: url,
                data: param,
                type: "POST",
                dataType: "json",
                async: true,
                cache: false,
                success: function (data) {
                    if (data.resp_code == learun.httpCode.exception) {
                        learun.httpErrorLog(data.resp_msg);
                        data.resp_msg = '系统异常，请联系管理员！';
                    }
                    callback(data);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    learun.httpErrorLog(textStatus);
                    callback(exres);
                },
                beforeSend: function () {
                },
                complete: function () {
                }
            });
        },
        // post请求方法（同步）:url地址,param参数,callback回调函数
        httpPost: function (url, param, callback) {
            $.ajax({
                url: url,
                data: param,
                type: "POST",
                dataType: "json",
                async: false,
                cache: false,
                success: function (data) {
                    if (data.resp_code == learun.httpCode.exception) {
                        learun.httpErrorLog(data.resp_msg);
                        data.resp_msg = '系统异常，请联系管理员！';
                    }
                    callback(data);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    learun.httpErrorLog(textStatus);
                    callback(exres);
                },
                beforeSend: function () {
                },
                complete: function () {
                }
            });
        },
        // delete请求方法（异步）:url地址,param参数,callback回调函数
        httpAsyncDelete: function (url, param, callback) {
            $.ajax({
                url: url,
                data: param,
                type: "DELETE",
                dataType: "json",
                contentType: 'application/json',
                async: true,
                cache: false,
                success: function (data) {
                    if (data.resp_code == learun.httpCode.exception) {
                        learun.httpErrorLog(data.resp_msg);
                        data.resp_msg = '系统异常，请联系管理员！';
                    }
                    callback(data);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    learun.httpErrorLog(textStatus);
                    callback(exres);
                },
                beforeSend: function () {
                },
                complete: function () {
                }
            });
        },
        // delete请求方法（同步步）:url地址,param参数,callback回调函数
        httpDelete: function (url, param, callback) {
            $.ajax({
                url: url,
                data: param,
                type: "DELETE",
                dataType: "json",
                async: false,
                cache: false,
                success: function (data) {
                    if (data.resp_code == learun.httpCode.exception) {
                        learun.httpErrorLog(data.resp_msg);
                        data.resp_msg = '系统异常，请联系管理员！';
                    }
                    callback(data);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    learun.httpErrorLog(textStatus);
                    callback(exres);
                },
                beforeSend: function () {
                },
                complete: function () {
                }
            });
        },
        // ajax 异步封装
        httpAsync: function (type, url, param, callback) {
            $.ajax({
                url: url,
                data: param,
                type: type,
                dataType: "json",
                async: true,
                cache: false,
                success: function (res) {
                    if (res.resp_code == 200) {
                        callback(res.data);
                    }
                    else {
                        learun.httpErrorLog(res.resp_msg);
                        callback(null);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    learun.httpErrorLog(textStatus);
                    callback(null);
                },
                beforeSend: function () {
                },
                complete: function () {
                    $('#lr_form_bg').fadeOut();
                }
            });
        },

        // ajax 异步封装
        httpAsync2: function (type, url, param, callback) {
            $.ajax({
                url: url,
                data: param,
                type: type,
                dataType: "json",
                async: true,
                cache: false,
                success: function (res) {
                    if (res.resp_code == 200) {
                        callback(res);
                    }
                    else {
                        learun.httpErrorLog(res.resp_msg);
                        callback(null);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    learun.httpErrorLog(textStatus);
                    callback(null);
                },
                beforeSend: function () {
                },
                complete: function () {
                }
            });
        },

        deleteForm: function (url, param, callback) {
            learun.loading(true, '正在删除数据');
            learun.httpAsyncDelete(url, param, function (res) {
                learun.loading(false);
                if (res.resp_code == 200) {
                    if (!!callback) {
                        callback(res);
                    }
                    learun.alert.success(res.resp_msg);
                }
                else {
                    learun.alert.error(res.resp_msg);
                    learun.httpErrorLog(res.resp_msg);
                }
                layer.close(layer.index);
            });
        },
        putForm: function (url, param, callback) {
            learun.loading(true, '正在提交数据');
            learun.httpAsync('PUT', url, param, function (res) {
                learun.loading(false);
                if (!!callback) {
                    callback(res);
                }
                layer.close(layer.index);
            });
        },
        postForm: function (url, param, callback) {
            learun.loading(true, '操作处理中...');
            learun.httpAsyncPost(url, param, function (res) {
                learun.loading(false);
                if (res.resp_code == 200) {
                    if (!!callback) {
                        callback(res);
                    }
                    learun.alert.success(res.resp_msg);
                }
                else {
                    learun.alert.error(res.resp_msg);
                    learun.httpErrorLog(res.resp_msg);
                }
                layer.close(layer.index);
            });
        },
        postForm2: function (url, param, callback) {
            learun.loading(true, '操作处理中...');
            learun.httpPost(url, param, function (res) {
                learun.loading(false);
                if (res.resp_code == 200) {
                    if (!!callback) {
                        callback(res);
                    }
                    learun.alert.success(res.resp_msg);
                }
                else {
                    learun.alert.error(res.resp_msg);
                    learun.httpErrorLog(res.resp_msg);
                }
                layer.close(layer.index);
            });
        },
        postForm3: function (url, param, callback) {
            learun.loading(true, '操作处理中...');
            learun.httpAsyncPost(url, param, function (res) {
                learun.loading(false);
                if (res.resp_code == 200) {
                    learun.alert.success(res.resp_msg);
                }
                else {
                    learun.alert.error(res.resp_msg);
                    learun.httpErrorLog(res.resp_msg);
                }
                layer.close(layer.index);
                if (!!callback) {
                    callback(res);
                }
            });
        },
    });

})(window.jQuery, top.learun);